package com.explorex.infra.puma.gateway.netty.rrmp;


import com.explorex.infra.puma.gateway.netty.rrmp.codec.MessageHeader;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author: JacceYang
 * @createTime: 2022/09/06 20:27
 * @description:
 */
@Component
public class MessageCenter {

    @Resource
    private List<BaseProcessor> baseProcessors;

    private BaseProcessor getProcessor(short type) {
        Optional<BaseProcessor> process = baseProcessors.stream().filter(ele -> ele.getMessageType() == type).findAny();
        return process.isPresent() ? process.get() : null;
    }

    public RrmpMessage processMessage(RTcpSession session, RrmpMessage msg) {
        MessageHeader header = msg.getHeader();
        BaseProcessor processor = getProcessor((short) header.getBusiness());
        return processor.process(session, msg);
    }

}
